package Server;

import java.io.*;
import java.net.*;
import java.security.KeyStore;
import javax.net.*;
import javax.net.ssl.*;
import javax.security.cert.X509Certificate;

public class Server extends ClassServer {

    private String docroot;

    private static int DefaultServerPort = 9999;

    /**
     * Constructs a ClassFileServer.
     *
     * @param path the path where the server locates files
     */
    public Server(ServerSocket ss, String docroot) throws IOException
    {
        super(ss);
        this.docroot = docroot;
    }


    /**
     * Main method to create the class server that reads
     * files. This takes two command line arguments, the
     * port on which the server accepts requests and the
     * root of the path. To start up the server: <br><br>
     *
     * <code>   java ClassFileServer <port> <path>
     * </code><br><br>
     *
     * <code>   new ClassFileServer(port, docroot);
     * </code>
     */
    public static void main(String args[])
    {
        System.out.println("Mysserver online!");

        int port = DefaultServerPort;
        String docroot = "/testfolder/";
        


        try {
            ServerSocketFactory ssf =
                Server.getServerSocketFactory("TLS");
            ServerSocket ss = ssf.createServerSocket(port);

                ((SSLServerSocket)ss).setNeedClientAuth(true);

            new Server(ss, docroot);
            

        } catch (IOException e) {
            System.out.println("Unable to start ClassServer: " +
                               e.getMessage());
            e.printStackTrace();
        }

    }

    private static ServerSocketFactory getServerSocketFactory(String type) {
        
            SSLServerSocketFactory ssf = null;
            try {
                // set up key manager to do server authentication
                SSLContext ctx;
                KeyManagerFactory kmf;
                TrustManagerFactory tmf;
                KeyStore ks;
                char[] passphrase = "f9kg4aA2".toCharArray();


                ctx = SSLContext.getInstance("TLS");
                kmf = KeyManagerFactory.getInstance("SunX509");
                ks = KeyStore.getInstance("JKS");

                ks.load(new FileInputStream("cert/Serverside/srvkey.jks"), passphrase);

                kmf.init(ks, passphrase);
                
                
                
                passphrase = "12QmaouY".toCharArray();
                
                tmf = TrustManagerFactory.getInstance("SunX509");
                ks = KeyStore.getInstance("JKS");
                ks.load(new FileInputStream("cert/Serverside/srvtrust.jks"), passphrase);
                tmf.init(ks);
                
                ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
                
                
                ssf = ctx.getServerSocketFactory();
                return ssf;
            } catch (Exception e) {
                e.printStackTrace();
            }
        
        return null;
    }
}
